﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using Delonix_Regia_Hotel_Management_System.Class;

namespace Delonix_Regia_Hotel_Management_System
{
    public partial class Room_Booking : Form
    {
        public Room_Booking()
        {
            InitializeComponent();
        }

        private void comboBoxChild_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label13_Click(object sender, EventArgs e)
        {

        }

        private void btnBook_Click(object sender, EventArgs e)
        {

            string bookingId = "B" + DateTime.Now.GetHashCode().ToString();
            string nric = tbxNRIC.Text;
            string lastName = tbxLastName.Text;
            string firstName = tbxFirstName.Text;
            int phoneNum = Convert.ToInt32(tbxContactNo.Text);
            string email = tbxEmail.Text;
            string address = tbxAddress.Text;
            int numOfAdults = Convert.ToInt32(comboBoxAdult.SelectedItem);
            int numOfChildren = Convert.ToInt32(comboBoxChild.SelectedItem);
            string roomType = Convert.ToString(comboBoxRoom.SelectedItem);
            string paymentMode = Convert.ToString(comboBoxPayment.SelectedItem);
            DateTime startDate = dateTimePicker1.Value;
            string checkInDate = Convert.ToString(dateTimePicker1.Value.Date.Year + "/" + dateTimePicker1.Value.Date.Month + "/" + dateTimePicker1.Value.Date.Day);
            string checkOutDate = Convert.ToString(dateTimePicker2.Value.Date.Year + "/" + dateTimePicker2.Value.Date.Month + "/" + dateTimePicker2.Value.Date.Day);
            DateTime endDate = dateTimePicker2.Value;
            string additionalRemarks = tbxRemark.Text;
            int compareDate = DateTime.Compare(startDate, endDate);

            Room newBooking = new Room();
            int roomAllocated = 0;
            lblBookingID.Text = bookingId;

            if (compareDate < 0)
            {
                if (DBManager.CheckRoomAvailability(checkInDate, checkOutDate, roomType) != null)
                {
                    newBooking = DBManager.CheckRoomAvailability(checkInDate, checkOutDate, roomType);
                    roomAllocated = newBooking.roomNum;
                    lblBookingID.Text = bookingId;


                    Guest g = new Guest(nric, lastName, firstName, phoneNum, address, email);
                    RoomBooking r = new RoomBooking(bookingId, nric, numOfAdults, numOfChildren, roomType, checkInDate, checkOutDate, additionalRemarks, paymentMode, roomAllocated);

                    if (DBManager.CreateRoomBooking(g, r) > 0)
                    {
                        MessageBox.Show("Booking successfully made!");
                        if (LogInClass.position == "Administrator")
                        {
                            Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                            this.Hide();
                            mainA.Show();
                        }
                        else
                        {
                            Main__Receptionist_ mainR = new Main__Receptionist_();
                            this.Hide();
                            mainR.Show();
                        }
                    }
                }
            }



        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (LogInClass.position == "Administrator")
            {
                Main__Management_and_Administrator_ mainA = new Main__Management_and_Administrator_();
                this.Hide();
                mainA.Show();
            }
            else
            {
                Main__Receptionist_ mainR = new Main__Receptionist_();
                this.Hide();
                mainR.Show();
            }
         }

        private void button2_Click(object sender, EventArgs e)
        {
            Form1 F = new Form1();
            this.Hide();
            F.Show(); 
        }


        
    }
}
